﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Markup;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml;
using System.Data.Services.Client;
using AbbrWordWikiDB;

namespace AbbrWordWiki
{
    /// <summary>
    /// Server=tcp:d443ho73va.database.windows.net;Database=AbbrWordWikiDB;User ID=zhaoz1@d443ho73va;Password=myPassword;Trusted_Connection=False;Encrypt=True;
    /// https://odata.sqlazurelabs.com/OData.svc/v0.1/d443ho73va/AbbrWordWikiDB
    /// </summary>
    public class MainViewModel : INotifyPropertyChanged
    {
        private AbbrWordWikiDB.AbbrWordWikiDBEntities context;

        public MainViewModel()
        {
            this.PropertyChanged += new PropertyChangedEventHandler(MainViewModel_PropertyChanged);

            IsDataLoaded = false;

            Uri DataSource = new Uri("https://odata.sqlazurelabs.com/OData.svc/v0.1/d443ho73va/AbbrWordWikiDB");
            
            //Uri DataSource = new Uri("http://localhost/AbbrWordWiki_OData/AbbrWordWiki_WcfDataService.svc");

            context = new AbbrWordWikiDB.AbbrWordWikiDBEntities(DataSource);

            LoadDataFromCloud();
            
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        
        public void LoadDataFromCloud()
        {
            var qry = from u in context.t_AbbrWordWiki
                      orderby u.FullWord
                      select u;

            var dsQry = (DataServiceQuery<t_AbbrWordWiki>)qry;

            dsQry.BeginExecute(r =>
            {
                try
                {
                    var result = dsQry.EndExecute(r).ToList();
                    if (result != null)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {   
                            //AbbrWords.Load(result);
                            AbbrWordsViewData = result;
                        });
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }, dsQry);
        }

        public void CreateNewWord(t_AbbrWordWiki newWord)
        {
            context.AddTot_AbbrWordWiki(newWord);
            context.BeginSaveChanges(r =>
            {
                try
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        DataServiceResponse response = context.EndSaveChanges(r);
                    });
                }
                catch
                {
                }
            }, context);

            App.ViewModel.AbbrWordsViewData.Add(newWord);
        }

        public void UpdateWord(t_AbbrWordWiki word)
        {
            t_AbbrWordWiki currentWord = 
                App.ViewModel.AbbrWordsViewData.Where(e => e.FullWord == word.FullWord).First<t_AbbrWordWiki>();

            currentWord.FullWord = word.FullWord;
            currentWord.Description = word.Description;

            context.UpdateObject(currentWord);
            context.BeginSaveChanges(r =>
            {
                try
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        DataServiceResponse response = context.EndSaveChanges(r);
                    });
                }
                catch
                {
                }
            }, context);
        }

        private List<t_AbbrWordWiki> _abbrWordsViewData = new List<t_AbbrWordWiki>();
        public List<t_AbbrWordWiki> AbbrWordsViewData
        {
            get
            {
                return _abbrWordsViewData;
            }
            set
            {
                _abbrWordsViewData = value;
                NotifyPropertyChanged("AbbrWordsViewData");
            }
        }

        DataServiceCollection<t_AbbrWordWiki> _abbrWords = new DataServiceCollection<t_AbbrWordWiki>();
        public DataServiceCollection<t_AbbrWordWiki> AbbrWords
        {
            get
            {
                return _abbrWords;
            }
            private set
            {
                _abbrWords = value;
                NotifyPropertyChanged("AbbrWords");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void MainViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.IsDataLoaded = true;
        }
    }
}